package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.hjq.widget.view.SwitchButton;
import com.qiantu.api.entity.GloBaLoPeRationDeviceBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.bean.TimeLimitBean;
import com.qiantu.phone.ui.dialog.BottomInoPeRationTimeDialog;
import com.qiantu.phone.ui.dialog.BottomTimeExtinguishDialog;
import com.qiantu.phone.utils.SPUtils;
import com.lxj.xpopup.XPopup;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

import okhttp3.Call;

/**
 * @Description: 指示灯设置
 * @Time: 2023年1月5日
 */

public class DeviceLedSetActivity extends AppActivity{
    private SwitchButton deviceLedSet_switch;
    private SwitchButton isSyncAll_switch;
    private LinearLayout inoPeration_time;
    private LinearLayout extinction_time;
    private TextView tv_extinction_time;
    private TextView tv_Inoperation_time;
    private String deviceSerialNos;
    private String houseSerialNo;
    private Boolean isSyncAll;
    private BottomTimeExtinguishDialog mBottomTimeExtinguishDialog;
    private BottomInoPeRationTimeDialog mBottomInoperationTimeDialog;
    private int startsleeptime;
    private int endsleeptime;
    private int waitsleeptime;
    private int enablesleep;  //0停 1启

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_le_dset;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        deviceLedSet_switch = findViewById(R.id.btn_DeviceLedSet_switch);
        isSyncAll_switch = findViewById(R.id.btn_isSyncAll_switch);
        inoPeration_time = findViewById(R.id.ll_Inoperation_time);
        extinction_time = findViewById(R.id.ll_Extinction_time);
        tv_extinction_time = findViewById(R.id.tv_extinction_time);
        tv_Inoperation_time = findViewById(R.id.tv_Inoperation_time);
        deviceSerialNos = intent.getStringExtra("deviceSerialNo");
        houseSerialNo = intent.getStringExtra("houseSerialNo");
        startsleeptime = intent.getIntExtra("startsleeptime",0);
        endsleeptime = intent.getIntExtra("endsleeptime",0);
        waitsleeptime = intent.getIntExtra("waitsleeptime",0);
        enablesleep = intent.getIntExtra("enablesleep",0);
        isSyncAll = intent.getBooleanExtra("isSyncAll", false);
        setOnClickListener(R.id.btn_DeviceLedSet_switch,R.id.btn_isSyncAll_switch,R.id.ll_Extinction_time,R.id.ll_Inoperation_time);
        upDataView();
    }

    private void upDataView() {
        isSyncAll_switch.setChecked(isSyncAll);

        boolean deviceLedSet_Switch = SPUtils.getInstance().getBoolean("deviceLedSet_switch", false);
        if (enablesleep==1){
            deviceLedSet_Switch=true;
        }
        if (deviceLedSet_Switch){
            enablesleep = 1;
            deviceLedSet_switch.setChecked(deviceLedSet_Switch);
            extinction_time.setVisibility(View.VISIBLE);
            inoPeration_time.setVisibility(View.VISIBLE);
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm", Locale.CHINA);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
        String format = simpleDateFormat.format(new Date(startsleeptime * 60 * 1000));
        String format2 = simpleDateFormat.format(new Date(endsleeptime * 60 * 1000));
        if (startsleeptime >= endsleeptime){
            tv_extinction_time.setText(format+"~次日" + format2);
        }else {
            tv_extinction_time.setText(format+"~" + format2);
        }

        if (waitsleeptime == 0){
            tv_Inoperation_time.setText("立即");
        }else {
            if (waitsleeptime < 60){
                tv_Inoperation_time.setText(waitsleeptime+"秒");
            }else {
                int waitsleeptime2 = waitsleeptime / 60;
                tv_Inoperation_time.setText(waitsleeptime2+"分钟");
            }
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.btn_DeviceLedSet_switch:
                if (deviceLedSet_switch.isChecked()){
                    enablesleep = 1;
                    extinction_time.setVisibility(View.VISIBLE);
                    inoPeration_time.setVisibility(View.VISIBLE);
                }else {
                    enablesleep = 0;
                    extinction_time.setVisibility(View.GONE);
                    inoPeration_time.setVisibility(View.GONE);
                }
                SPUtils.getInstance().put("deviceLedSet_switch",deviceLedSet_switch.isChecked());
                getGloBaLoPeRationDevice();
                break;
            case R.id.ll_Extinction_time:
                showBottomTimeExtinguishDialog();
                break;
            case R.id.ll_Inoperation_time:
                showBottomInoperationTimeDialog();
                break;
            case R.id.btn_isSyncAll_switch:
                isSyncAll = isSyncAll_switch.isChecked();
                getGloBaLoPeRationDevice();
                break;
        }
    }
    @Override
    protected void onPause() {
        super.onPause();
    }



    private void getGloBaLoPeRationDevice() {
        String[] deviceTypeList = {"wall_switch_J4","wall_switch_J8"};
        Map<String,Object> deviceItem=new HashMap<>();
        Map<String,Object> stateData=new HashMap<>();
        Map<String,Object> settings=new HashMap<>();
        settings.put("startsleeptime",startsleeptime);
        settings.put("endsleeptime",endsleeptime);
        settings.put("waitsleeptime",waitsleeptime);
        settings.put("enablesleep",enablesleep);
        stateData.put("settings",settings);
        deviceItem.put("deviceSerialNo",deviceSerialNos);
        deviceItem.put("stateData",stateData);
        LLHttpManager.getGloBaLopeRationDevice(this,houseSerialNo,deviceItem,isSyncAll,deviceTypeList,new HttpCallback<HttpData<GloBaLoPeRationDeviceBean>>(this){
            @Override
            public void onStart(Call call) {
                super.onStart(call);
            }

            @Override
            public void onSucceed(HttpData<GloBaLoPeRationDeviceBean> result) {
                super.onSucceed(result);
                if (result!=null){
                    ToastUtils.show("设置成功");
                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                super.onEnd(call);
            }
        });
    }


    //熄灭时间
    private void showBottomTimeExtinguishDialog(){
        if (mBottomTimeExtinguishDialog == null) {
            mBottomTimeExtinguishDialog = new BottomTimeExtinguishDialog(this);
            mBottomTimeExtinguishDialog.setDialogClickListener(new BottomTimeExtinguishDialog.DialogClickListener() {
                @Override
                public void onConfirmClick(boolean isOpen, String startTime, String endTime) {
                    setLockTime(startTime,endTime);
                    mBottomTimeExtinguishDialog.dismiss();
                }
            });
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomTimeExtinguishDialog);
        }
        String period = tv_extinction_time.getText().toString();
        String start = null;
        String end = null;
        boolean isOpen;
        if(period.contains("~")){
            isOpen = true;
            start = period.split("~")[0];
            end = period.split("~")[1].replace("次日", "");
        }else{
            isOpen = false;
            start = "00:00";
            end = "00:00";
        }
        mBottomTimeExtinguishDialog.setSelectedTime(isOpen, start, end);
        mBottomTimeExtinguishDialog.show();
    }

    //不操作时间
    private void showBottomInoperationTimeDialog(){
        if (mBottomInoperationTimeDialog == null) {
            mBottomInoperationTimeDialog = new BottomInoPeRationTimeDialog(this);
            mBottomInoperationTimeDialog.setDialogClickListener(new BottomInoPeRationTimeDialog.DialogClickListener() {
                @Override
                public void onConfirmClick(TimeLimitBean timeLimitBean) {
                    tv_Inoperation_time.setText(timeLimitBean.getLabel());
                    waitsleeptime = Integer.parseInt(timeLimitBean.getValue());
                    mBottomInoperationTimeDialog.dismiss();
                    getGloBaLoPeRationDevice();
                }
            });
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomInoperationTimeDialog);
        }
        mBottomInoperationTimeDialog.show();
    }

    //开始时间和结束时间判断
    private void setLockTime(String start, String end){
        if(TextUtils.isEmpty(start) || TextUtils.isEmpty(end)){
            return;
        }
        String[] startHM = start.split(":");
        String[] endHM = end.split(":");
        int startH = Integer.valueOf(startHM[0]);
        int startM = Integer.valueOf(startHM[1]);
        int endH = Integer.valueOf(endHM[0]);
        int endM = Integer.valueOf(endHM[1]);
        String nextDay = "";
        //结束时间小于开始时间
        if(startH >= endH || (startH == endH && startM >= endM)){
            nextDay = "次日";
        }
        tv_extinction_time.setText(start +  "~" + nextDay + end);
        startsleeptime = (startH * 60 + startM);
        endsleeptime = (endH * 60 + endM);
        getGloBaLoPeRationDevice();
    }
}